using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Hmw;
using net_work.Data.Entity.Hmw;
using net_work.IRepository.Hmw;
using net_work.IService.Hmw;

namespace net_work.Service.Hmw;

public class BankAnswerService(
    IBankAnswerRepository bankAnswerRepository,
    IBankQuestionRepository bankQuestionRepository)
    : IBankAnswerService
{
    public Task<PagedResult<BankAnswer>> GetBankAnswersPagedAsync(PagedRequest request)
    {
        return bankAnswerRepository.GetPagedAsync(request);
    }

    public Task<BankAnswer?> SelectByIdAsync(long bankAnswerId)
    {
        return bankAnswerRepository.GetByIdAsync(bankAnswerId);
    }

    public async Task<long> InsertBankAnswer(BankAnswerAddRequest bankAnswerAddRequest)
    {
        var question = await bankQuestionRepository.GetByIdAsync(bankAnswerAddRequest.QuestionId);
        if (question == null)
        {
            return -1;
        }

        return await bankAnswerRepository.InsertAsync(bankAnswerAddRequest);
    }

    public async Task<int> UpdateBankAnswer(BankAnswerUpdateRequest bankAnswerUpdateRequest)
    {
        if (bankAnswerUpdateRequest.Id <= 0)
        {
            return -1;
        }

        var question = await bankQuestionRepository.GetByIdAsync(bankAnswerUpdateRequest.QuestionId);
        if (question == null)
        {
            return -1;
        }

        return await bankAnswerRepository.UpdateAsync(bankAnswerUpdateRequest) ? 1 : -1;
    }

    public async Task<int> DeleteBankAnswerByIds(List<long> ids)
    {
        return await bankAnswerRepository.DeleteByIdsAsync(ids) ? 1 : -1;
    }
}